package cz.jm.web.manhunter.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.layout.client.Layout.Alignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

import cz.jm.web.jmlib.client.layouts.VerticalLayout;

public class AppView implements AppController.Display {
	
	private final AppConstants constants = GWT.create(AppConstants.class);
	

	private static final String MENU_BUTTON_WIDTH = "100px";
	private static final int MENU_BUTTON_PADDING = 0;
	private static final int MENU_BUTTON_MARGIN = 2;
	
	private static final String APPLICATION_BG_COLOR = "white";
	private static final String HEADER_BG_COLOR = "#3b5998";
	private static final String BODY_BG_COLOR = "white";
	private static final String FOOTER_BG_COLOR = "white";

	private static final double HEADER_HEIGHT = 2;
	private static final Unit HEADER_HEIGHT_UNIT = Unit.EM;
	/** Sirka menu ktoru zaberaz z hlavicky v percentach. */
	private static final int HEADER_MENU_WIDTH_PERCETAGE = 90;
	
	private static final int FOOTER_HEIGHT = 2;
	private static final Unit FOOTER_HEIGHT_UNIT = Unit.EM;
	
	private final VerticalLayout mainLayout = new VerticalLayout();
	private final LayoutPanel workingAreaLayout = new LayoutPanel();
	
	private final Button profileButton = new Button(constants.profile());
	private final Button skillsButton = new Button(constants.skills());
	private final Button aboutButton = new Button(constants.about());
	private final Button loggInButton = new Button(constants.loggIn());
	private final Button registrationButton = new Button(constants.registration());
	private final ListBox languageSelector = new ListBox();

	
	public AppView() {

		mainLayout.getElement().getStyle().setBackgroundColor(APPLICATION_BG_COLOR);
		
		// Nastavenie zahlavia == menu + zmena jazyka aplikacie
		final LayoutPanel headerLayout = new LayoutPanel();
		mainLayout.add(headerLayout);		
		headerLayout.getElement().getStyle().setBackgroundColor(HEADER_BG_COLOR);
		// Pozicia hlavicky je uplene hore.		
		mainLayout.setWidgetTopHeight(headerLayout, 0, Unit.PX, HEADER_HEIGHT, HEADER_HEIGHT_UNIT);
		
		// Vytvorenie menu.
		final FlowPanel menuLayout = new FlowPanel();		
		initMenuButton(profileButton);
		menuLayout.add(profileButton);		
		initMenuButton(skillsButton);
		menuLayout.add(skillsButton);		
		initMenuButton(aboutButton);
		menuLayout.add(aboutButton);		
		initMenuButton(loggInButton);
		menuLayout.add(loggInButton);		
		initMenuButton(registrationButton);		
		menuLayout.add(registrationButton);
		headerLayout.add(menuLayout);
		// Menu je uplne vlavo a zabera 90% zahlavia.
		headerLayout.setWidgetLeftWidth(menuLayout, 0, Unit.PCT, HEADER_MENU_WIDTH_PERCETAGE, Unit.PCT);
		// Vytvorenie vyberu jazykovych mutacii.
		String locales[] = LocaleInfo.getAvailableLocaleNames();
		String actLocale = LocaleInfo.getCurrentLocale().getLocaleName();
		languageSelector.addItem(actLocale, actLocale);
		for (String locale : locales) {
			if (!(locale.equals(actLocale) || locale.equals("default"))) {
				languageSelector.addItem(locale, locale);
			}
		}		
		headerLayout.add(languageSelector);
		// Vyber jazyka je uplne vpravo v zahlavi.
		headerLayout.setWidgetRightWidth(languageSelector, 0, Unit.PCT, 100 - HEADER_MENU_WIDTH_PERCETAGE, Unit.PCT);
		headerLayout.setWidgetHorizontalPosition(languageSelector, Alignment.END);
		// Nastavenie tela == pracovnej plochy aplikacie.
		mainLayout.add(workingAreaLayout);
		workingAreaLayout.getElement().getStyle().setBackgroundColor(BODY_BG_COLOR);	
		// Pracovna plocha zacina hned pod hlavickou a roztiahne sa na 100% neobsadenej plochy.
		// Tj. bude zmensena este o paticku stranky (ak bude).		
		mainLayout.setWidgetTopHeight(workingAreaLayout, HEADER_HEIGHT, HEADER_HEIGHT_UNIT, 100, Unit.PCT);
		// Vytvorenie paticky stranky.
		final LayoutPanel footerLayout = new LayoutPanel();
		mainLayout.add(footerLayout);
		footerLayout.getElement().getStyle().setBackgroundColor(FOOTER_BG_COLOR);
		// Paticka je uplne dole a na celu sirku stranky.		
		mainLayout.setWidgetBottomHeight(footerLayout, 0, Unit.PCT, FOOTER_HEIGHT, FOOTER_HEIGHT_UNIT);
		Label fl = new Label("Majdo (C) Todo ...");
		footerLayout.add(fl);
	}
	

	private void initMenuButton(Button button) {
		button.setWidth(MENU_BUTTON_WIDTH);
		button.getElement().getStyle().setPadding(MENU_BUTTON_PADDING, Unit.PX);
		button.getElement().getStyle().setMargin(MENU_BUTTON_MARGIN, Unit.PX);
	}
	

	@Override
	public HasClickHandlers getProfile() {
		return profileButton;
	}

	@Override
	public HasClickHandlers getSkills() {
		return skillsButton;
	}

	@Override
	public HasClickHandlers getAbout() {
		return aboutButton;
	}

	@Override
	public HasClickHandlers getLoggIn() {
		return loggInButton;
	}


	@Override
	public HasClickHandlers getRegistration() {
		return registrationButton;
	}

	@Override
	public HasChangeHandlers getLanguageSelector() {
		return languageSelector;
	}

	@Override
    public String getLanguage() {
	    return languageSelector.getValue(languageSelector.getSelectedIndex());
    }

	@Override
    public Widget asWidget() {
	    return mainLayout;
    }

	@Override
    public LayoutPanel getWorkingArea() {
	    return workingAreaLayout;
    }
}
